package org.hilo.boot.scaffold;

import org.hilo.boot.core.UT;

/**
 * 开发框架 Demo示例项目生成器
 * 
 * @author zollty
 * @since 2020-5-16
 */
public class DemoProjectGenerator extends ScaffoldGenerator {
    
    public DemoProjectGenerator() {
        super();
        this.init();
    }
    
    public DemoProjectGenerator(String groupId, String artifactId, String basePackage) {
        super(groupId, artifactId, basePackage);
        this.init();
    }

    private void init() {
        demoMode = true;
        sourceProjectName = "hilodemo-all";
        sourceFileUrl = "http://10.2.10.51:8081/nexus/content/repositories/releases/org/hilo/framework/hilodemo-all-source/1/hilodemo-all-source-1.zip";
    }
    
    @Override
    void commonBuild(String target) {
        deleteIDEFiles(target);
        deleteIDEFiles(target + artifactId + "/");
        deleteIDEFiles(target + artifactId + "-biz-db/");
        deleteIDEFiles(target + artifactId + "-common/");
        deleteIDEFiles(target + artifactId + "-biz-account/");
        deleteIDEFiles(target + artifactId + "-biz-payment/");
        deleteIDEFiles(target + artifactId + "-api/");
        
        Rep.build(target + artifactId + "/src/main/resources/application-dev.yml", null)
        .replace("password: Pub#1234", "password: 123456").apply();
        
        deleteAll(target + "pom.xml");
        renameTo(target + "pom-app.xml", target + "pom.xml");
        
        String ts1 = "\r\n    <dependency>\r\n      <groupId>" + groupId
                + "</groupId>\r\n      <artifactId>" + artifactId
                + "-biz-db</artifactId>\r\n    </dependency>";
        Rep.build(target + artifactId + "-biz-account/pom.xml", null)
        .deleteLine(ts1).apply();
        
        UT.File.copyFolder(target + artifactId + "-biz-db/src", target + artifactId + "-biz-account/src", false);
        deleteAll(target + artifactId + "-biz-db");
    }
    
    @Override
    void buildAdmin(String target, String newPath) {
        String subTarget = target + artifactId + "/";
        deleteAll(subTarget+"src/main/java/" + newPath + "/StartApplicationDubbo.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/StartApplicationAdmin.java");
        Rep.build(subTarget+"src/main/resources/application.yml", null)
        .replace("hilo-demo", artifactId)
        .apply();
    }
    
    void buildAdmin2(String target, String newPath) {
        String subTarget = target + artifactId + "-biz/";
        deleteAll(subTarget + "pom-dubbo.xml");
        renameTo(subTarget + "pom-admin.xml", subTarget + "pom.xml");
        
        subTarget = target + artifactId + "/";
        Rep.build(subTarget+"src/main/java/" + newPath + "/StartApplicationAdmin.java", 
                subTarget+"src/main/java/" + newPath + "/StartApplication.java")
        .replace("StartApplicationAdmin", "StartApplication").apply();
        deleteAll(subTarget+"src/main/java/" + newPath + "/StartApplicationDubbo.java");
        
        deleteAll(subTarget + "src/main/java/" + newPath + "/app/RedisConfigFoo2.java");
        
        Rep.build(subTarget+"src/main/resources/META-INF/app.properties", null)
        .replace("fast4ui", artifactId).apply();
    
        Rep.build(subTarget+"src/main/resources/application.yml", null)
            .replace("hilo-demo", artifactId)
            .replace("base,dbbase,dbweb,admin,dubbo,dev", "base,dbbase,dbweb,admin,dev")
            .apply();
        
        deleteAll(subTarget + "src/main/resources/application-dubbo.yml");
        
        //subTarget = target + artifactId + "-api/";
        //deleteAll(subTarget);
    }
    
    @Override
    void buildDubbo(String target, String newPath) {
        buildAdmin(target, newPath);
    }
    
    void buildDubbo2(String target, String newPath) {
        String subTarget = target + artifactId + "-biz/";
        deleteAll(subTarget + "pom-admin.xml");
        deleteAll(subTarget + "pom-web.xml");
        deleteAll(subTarget + "pom-web-db.xml");
        renameTo(subTarget + "pom-dubbo.xml", subTarget + "pom.xml");
        
        subTarget = target + artifactId + "/";
        Rep.build(subTarget+"src/main/java/" + newPath + "/StartApplicationDubbo.java", 
                subTarget+"src/main/java/" + newPath + "/StartApplication.java")
        .replace("StartApplicationDubbo", "StartApplication").apply();
        deleteAll(subTarget+"src/main/java/" + newPath + "/StartApplicationAdmin.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/StartApplicationDB.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/app/ShiroFilterConfig.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/app/RedisConfigFoo2.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/app/AppExceptionResolver.java");
        deleteAll(subTarget+"src/main/java/" + newPath + "/app/WebMvcConfig.java");
        
        Rep.build(subTarget + "src/main/resources/application-dubbo.yml", null)
        .replace("hilo-demo", artifactId).apply();
    }
    
    @Override
    void buildWeb(String target, String newPath) {
        
    }


}
